package com.liljhu30.microdianping.utils;

import com.liljhu30.microdianping.dto.LoginFormDTO;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author LilJHu
 * @create 2024/1/5
 * @desc 返回值处理器：负责处理HTTP请求和响应中的消息转换
 **/
public class CustomHttpMessageConverter implements HttpMessageConverter<LoginFormDTO> {

    @Override
    public boolean canRead(Class<?> clazz, MediaType mediaType) {
        // 判断是否支持读取
        return false;
    }

    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        // 判断是否支持写入
        return LoginFormDTO.class.isAssignableFrom(clazz);
    }

    @Override
    public List<MediaType> getSupportedMediaTypes() {
        // 返回支持的媒体类型
        return Arrays.asList(MediaType.TEXT_PLAIN);
    }

    @Override
    public LoginFormDTO read(Class<? extends LoginFormDTO> clazz, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException {
        // 实现读取逻辑（如果需要的话）
        return null;
    }

    @Override
    public void write(LoginFormDTO loginFormDTO, MediaType contentType, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        // 实现写入逻辑
        String value = loginFormDTO != null ? loginFormDTO.getCode() : "null";
        outputMessage.getBody().write(value.getBytes());
    }

}
